' Assigns a result to a battle. The default is "Not Finished"

Imports cmplib01

Public Class frmCombatResolutions
    Inherits System.Windows.Forms.Form

    Private ptrGameData As CampaignerGameData
    Private ptrSelectedBattle As Battle

    Private bSelfUpdate As Boolean = False    ' used to keep the 'checked' property on the option boxes from firing at certain times.

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents btnOK As System.Windows.Forms.Button
    Friend WithEvents pnlTopBorder As System.Windows.Forms.Panel
    Friend WithEvents picCampaignerIcon As System.Windows.Forms.PictureBox
    Friend WithEvents txtTitleBox As System.Windows.Forms.TextBox
    Friend WithEvents lblBattles As System.Windows.Forms.Label
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    Friend WithEvents lblOutcome As System.Windows.Forms.Label
    Friend WithEvents optDraw As System.Windows.Forms.RadioButton
    Friend WithEvents optAttackerVictory As System.Windows.Forms.RadioButton
    Friend WithEvents optDefenderVictory As System.Windows.Forms.RadioButton
    Friend WithEvents optAttackerMarginalVictory As System.Windows.Forms.RadioButton
    Friend WithEvents optDefenderMarginalVictory As System.Windows.Forms.RadioButton
    Friend WithEvents optNotFinished As System.Windows.Forms.RadioButton
    Friend WithEvents imgCheckNoCheck As System.Windows.Forms.ImageList
    Friend WithEvents tvwBattles As System.Windows.Forms.TreeView
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmCombatResolutions))
        Me.btnOK = New System.Windows.Forms.Button()
        Me.pnlTopBorder = New System.Windows.Forms.Panel()
        Me.picCampaignerIcon = New System.Windows.Forms.PictureBox()
        Me.txtTitleBox = New System.Windows.Forms.TextBox()
        Me.lblBattles = New System.Windows.Forms.Label()
        Me.Panel1 = New System.Windows.Forms.Panel()
        Me.tvwBattles = New System.Windows.Forms.TreeView()
        Me.imgCheckNoCheck = New System.Windows.Forms.ImageList(Me.components)
        Me.optNotFinished = New System.Windows.Forms.RadioButton()
        Me.optDefenderMarginalVictory = New System.Windows.Forms.RadioButton()
        Me.optAttackerMarginalVictory = New System.Windows.Forms.RadioButton()
        Me.optDefenderVictory = New System.Windows.Forms.RadioButton()
        Me.optAttackerVictory = New System.Windows.Forms.RadioButton()
        Me.optDraw = New System.Windows.Forms.RadioButton()
        Me.lblOutcome = New System.Windows.Forms.Label()
        Me.pnlTopBorder.SuspendLayout()
        Me.Panel1.SuspendLayout()
        Me.SuspendLayout()
        '
        'btnOK
        '
        Me.btnOK.BackColor = System.Drawing.Color.LightSkyBlue
        Me.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnOK.ForeColor = System.Drawing.Color.Black
        Me.btnOK.Location = New System.Drawing.Point(328, 360)
        Me.btnOK.Name = "btnOK"
        Me.btnOK.TabIndex = 18
        Me.btnOK.Text = "OK"
        '
        'pnlTopBorder
        '
        Me.pnlTopBorder.BackColor = System.Drawing.Color.SteelBlue
        Me.pnlTopBorder.Controls.AddRange(New System.Windows.Forms.Control() {Me.picCampaignerIcon, Me.txtTitleBox})
        Me.pnlTopBorder.Name = "pnlTopBorder"
        Me.pnlTopBorder.Size = New System.Drawing.Size(418, 40)
        Me.pnlTopBorder.TabIndex = 17
        '
        'picCampaignerIcon
        '
        Me.picCampaignerIcon.Image = CType(resources.GetObject("picCampaignerIcon.Image"), System.Drawing.Bitmap)
        Me.picCampaignerIcon.Location = New System.Drawing.Point(9, 11)
        Me.picCampaignerIcon.Name = "picCampaignerIcon"
        Me.picCampaignerIcon.Size = New System.Drawing.Size(16, 16)
        Me.picCampaignerIcon.TabIndex = 9
        Me.picCampaignerIcon.TabStop = False
        '
        'txtTitleBox
        '
        Me.txtTitleBox.BackColor = System.Drawing.Color.SteelBlue
        Me.txtTitleBox.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.txtTitleBox.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.txtTitleBox.ForeColor = System.Drawing.Color.WhiteSmoke
        Me.txtTitleBox.Location = New System.Drawing.Point(32, 8)
        Me.txtTitleBox.Multiline = True
        Me.txtTitleBox.Name = "txtTitleBox"
        Me.txtTitleBox.ReadOnly = True
        Me.txtTitleBox.Size = New System.Drawing.Size(312, 26)
        Me.txtTitleBox.TabIndex = 8
        Me.txtTitleBox.Text = "Campaigner [Battle Resolution]"
        '
        'lblBattles
        '
        Me.lblBattles.BackColor = System.Drawing.Color.LightSkyBlue
        Me.lblBattles.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblBattles.ForeColor = System.Drawing.Color.Black
        Me.lblBattles.Location = New System.Drawing.Point(16, 5)
        Me.lblBattles.Name = "lblBattles"
        Me.lblBattles.Size = New System.Drawing.Size(112, 16)
        Me.lblBattles.TabIndex = 2
        Me.lblBattles.Text = "Battles"
        '
        'Panel1
        '
        Me.Panel1.BackColor = System.Drawing.Color.LightSkyBlue
        Me.Panel1.Controls.AddRange(New System.Windows.Forms.Control() {Me.tvwBattles, Me.optNotFinished, Me.optDefenderMarginalVictory, Me.optAttackerMarginalVictory, Me.optDefenderVictory, Me.optAttackerVictory, Me.optDraw, Me.lblOutcome, Me.lblBattles})
        Me.Panel1.Location = New System.Drawing.Point(8, 48)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(402, 304)
        Me.Panel1.TabIndex = 20
        '
        'tvwBattles
        '
        Me.tvwBattles.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.tvwBattles.ImageList = Me.imgCheckNoCheck
        Me.tvwBattles.Location = New System.Drawing.Point(8, 32)
        Me.tvwBattles.Name = "tvwBattles"
        Me.tvwBattles.Nodes.AddRange(New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("1st Mechanized vs Firebase Zeggerl", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Attacker", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Units")}), New System.Windows.Forms.TreeNode("Defender", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Installations"), New System.Windows.Forms.TreeNode("Units")})})})
        Me.tvwBattles.ShowLines = False
        Me.tvwBattles.Size = New System.Drawing.Size(208, 256)
        Me.tvwBattles.TabIndex = 11
        '
        'imgCheckNoCheck
        '
        Me.imgCheckNoCheck.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit
        Me.imgCheckNoCheck.ImageSize = New System.Drawing.Size(16, 16)
        Me.imgCheckNoCheck.ImageStream = CType(resources.GetObject("imgCheckNoCheck.ImageStream"), System.Windows.Forms.ImageListStreamer)
        Me.imgCheckNoCheck.TransparentColor = System.Drawing.Color.Transparent
        '
        'optNotFinished
        '
        Me.optNotFinished.Checked = True
        Me.optNotFinished.Location = New System.Drawing.Point(224, 32)
        Me.optNotFinished.Name = "optNotFinished"
        Me.optNotFinished.Size = New System.Drawing.Size(136, 16)
        Me.optNotFinished.TabIndex = 10
        Me.optNotFinished.TabStop = True
        Me.optNotFinished.Text = "Not Finished"
        '
        'optDefenderMarginalVictory
        '
        Me.optDefenderMarginalVictory.Location = New System.Drawing.Point(224, 128)
        Me.optDefenderMarginalVictory.Name = "optDefenderMarginalVictory"
        Me.optDefenderMarginalVictory.Size = New System.Drawing.Size(160, 16)
        Me.optDefenderMarginalVictory.TabIndex = 9
        Me.optDefenderMarginalVictory.Text = "Defender Marginal Victory"
        '
        'optAttackerMarginalVictory
        '
        Me.optAttackerMarginalVictory.Location = New System.Drawing.Point(224, 104)
        Me.optAttackerMarginalVictory.Name = "optAttackerMarginalVictory"
        Me.optAttackerMarginalVictory.Size = New System.Drawing.Size(160, 16)
        Me.optAttackerMarginalVictory.TabIndex = 8
        Me.optAttackerMarginalVictory.Text = "Attacker Marginal Victory"
        '
        'optDefenderVictory
        '
        Me.optDefenderVictory.Location = New System.Drawing.Point(224, 80)
        Me.optDefenderVictory.Name = "optDefenderVictory"
        Me.optDefenderVictory.Size = New System.Drawing.Size(136, 16)
        Me.optDefenderVictory.TabIndex = 7
        Me.optDefenderVictory.Text = "Defender Victory"
        '
        'optAttackerVictory
        '
        Me.optAttackerVictory.Location = New System.Drawing.Point(224, 56)
        Me.optAttackerVictory.Name = "optAttackerVictory"
        Me.optAttackerVictory.Size = New System.Drawing.Size(136, 16)
        Me.optAttackerVictory.TabIndex = 6
        Me.optAttackerVictory.Text = "Attacker Victory"
        '
        'optDraw
        '
        Me.optDraw.Location = New System.Drawing.Point(224, 152)
        Me.optDraw.Name = "optDraw"
        Me.optDraw.Size = New System.Drawing.Size(136, 16)
        Me.optDraw.TabIndex = 5
        Me.optDraw.Text = "Draw"
        '
        'lblOutcome
        '
        Me.lblOutcome.BackColor = System.Drawing.Color.LightSkyBlue
        Me.lblOutcome.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblOutcome.ForeColor = System.Drawing.Color.Black
        Me.lblOutcome.Location = New System.Drawing.Point(224, 8)
        Me.lblOutcome.Name = "lblOutcome"
        Me.lblOutcome.Size = New System.Drawing.Size(168, 16)
        Me.lblOutcome.TabIndex = 3
        Me.lblOutcome.Text = "Battle Outcome"
        '
        'frmCombatResolutions
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackColor = System.Drawing.Color.Black
        Me.ClientSize = New System.Drawing.Size(416, 392)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Panel1, Me.btnOK, Me.pnlTopBorder})
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow
        Me.Name = "frmCombatResolutions"
        Me.pnlTopBorder.ResumeLayout(False)
        Me.Panel1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

#Region "Hidden Members"
    Private Shadows Sub Show()

    End Sub

    Private Shadows Sub ShowDialog()

    End Sub

#End Region

    Public Shadows Sub ShowDialog(ByRef gameData As CampaignerGameData)
        ptrGameData = gameData

        If ptrGameData.Battles_g.HT.Count = 0 Then
            Me.Hide()
        End If

        Try
            PopulateBattles()
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try

        MyBase.ShowDialog()
    End Sub

    Private Sub PopulateBattles()
        Dim battKey As String
        Dim batt As Battle

        If tvwBattles.Nodes.Count > 0 Then tvwBattles.Nodes.Clear()

        For Each battKey In ptrGameData.Battles_g.HT.Keys
            batt = ptrGameData.Battles_g.Item(battKey)
            Dim nod As New BattleTreeNode(batt.BattleName)
            If batt.BattleResult = BattleResult_cgd.NotFinished Then
                nod.ImageIndex = 0
                nod.SelectedImageIndex = 0
            Else
                nod.ImageIndex = 1
                nod.SelectedImageIndex = 1
            End If
            AddSubNodes(batt, nod)
            nod.Tag = batt.Key
            tvwBattles.Nodes.Add(nod)
        Next

    End Sub

    Private Sub AddSubNodes(ByRef batt As Battle, ByRef node As BattleTreeNode)
        'Dim gcKey As String
        'Dim gc As GroundChitType
        Dim Attacker As String = "Attacker: " & ptrGameData.Faction_g.Item("FACT" & batt.AttackingFactionID).FactionName
        Dim Defender As String = "Defender: " & ptrGameData.Faction_g.Item("FACT" & batt.DefendingFactionID).FactionName
        Dim attackerNode As New TreeNode(Attacker)
        Dim attackerUnitsNode As New TreeNode("Defender: ")
        Dim defenderNode As New TreeNode("Defender: ")
        Dim defenderInstallationsNode As New TreeNode("Installations")
        Dim defenderUnitsNode As New TreeNode("Units")

        attackerNode.ImageIndex = 2
        attackerNode.SelectedImageIndex = 2
        defenderNode.ImageIndex = 2
        defenderNode.SelectedImageIndex = 2

        For Each mem As ChitMember In batt.InvolvedChits
            Dim newNode As New TreeNode()

            If mem.OwningFaction.FactionID = batt.AttackingFactionID Then
                newNode.Text = mem.Name
                newNode.ImageIndex = 3
                newNode.SelectedImageIndex = 3
                attackerNode.Nodes.Add(newNode)
            Else
                newNode.Text = mem.Name
                If mem.UnitType = GroundChitClass_cgd.Objective Then
                    newNode.ImageIndex = 4
                    newNode.SelectedImageIndex = 4
                    defenderNode.Nodes.Add(newNode)
                Else
                    newNode.ImageIndex = 3
                    newNode.SelectedImageIndex = 3
                    defenderNode.Nodes.Add(newNode)
                End If
            End If

        Next mem

        node.Nodes.Add(attackerNode)
        node.Nodes.Add(defenderNode)

    End Sub

    Private Sub PopulateInvolvedPlayers()
        If ptrSelectedBattle Is Nothing Then Exit Sub

        Dim attackFact As Faction = ptrGameData.Faction_g.Item(CampaignerGameData.CDGKEY_FACTION & ptrSelectedBattle.AttackingFactionID)
        Dim defendFact As Faction = ptrGameData.Faction_g.Item(CampaignerGameData.CDGKEY_FACTION & ptrSelectedBattle.DefendingFactionID)

    End Sub

    Private Sub ShowCombatResult()
        ' marks the correct option box with TRUE depending on the result already in the battle object
        If ptrSelectedBattle Is Nothing Then Exit Sub

        bSelfUpdate = True

        Select Case ptrSelectedBattle.BattleResult
            Case BattleResult_cgd.NotFinished : optNotFinished.Checked = True
            Case BattleResult_cgd.AttackerVictory : optAttackerVictory.Checked = True
            Case BattleResult_cgd.DefenderVictory : optDefenderVictory.Checked = True
            Case BattleResult_cgd.AttackerMarginalVictory : optAttackerMarginalVictory.Checked = True
            Case BattleResult_cgd.DefenderMarginalVictory : optDefenderMarginalVictory.Checked = True
            Case BattleResult_cgd.Draw : optDraw.Checked = True
            Case Else
                optNotFinished.Checked = True
        End Select

        bSelfUpdate = False

    End Sub

    Private Sub tvwBattles_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvwBattles.AfterSelect
        ' set the current battle
        If e.Node Is Nothing Then Exit Sub
        ptrSelectedBattle = ptrGameData.Battles_g.Item(e.Node.Tag)
        ShowCombatResult()
    End Sub

    Private Sub optNotFinished_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optNotFinished.CheckedChanged
        If bSelfUpdate = True Then Exit Sub
        If ptrSelectedBattle Is Nothing Then Exit Sub
        ptrSelectedBattle.BattleResult = BattleResult_cgd.NotFinished

    End Sub

    Private Sub optAttackerVictory_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optAttackerVictory.CheckedChanged
        If bSelfUpdate = True Then Exit Sub
        If ptrSelectedBattle Is Nothing Then Exit Sub
        ptrSelectedBattle.BattleResult = BattleResult_cgd.AttackerVictory
        PopulateBattles()
    End Sub

    Private Sub optDefenderVictory_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optDefenderVictory.CheckedChanged
        If bSelfUpdate = True Then Exit Sub
        If ptrSelectedBattle Is Nothing Then Exit Sub
        ptrSelectedBattle.BattleResult = BattleResult_cgd.DefenderVictory
        PopulateBattles()
    End Sub

    Private Sub optAttackerMarginalVictory_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optAttackerMarginalVictory.CheckedChanged
        If bSelfUpdate = True Then Exit Sub
        If ptrSelectedBattle Is Nothing Then Exit Sub
        ptrSelectedBattle.BattleResult = BattleResult_cgd.AttackerMarginalVictory
        PopulateBattles()
    End Sub

    Private Sub optDefenderMarginalVictory_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optDefenderMarginalVictory.CheckedChanged
        If bSelfUpdate = True Then Exit Sub
        If ptrSelectedBattle Is Nothing Then Exit Sub
        ptrSelectedBattle.BattleResult = BattleResult_cgd.DefenderMarginalVictory
        PopulateBattles()
    End Sub

    Private Sub optDraw_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optDraw.CheckedChanged
        If bSelfUpdate = True Then Exit Sub
        If ptrSelectedBattle Is Nothing Then Exit Sub
        ptrSelectedBattle.BattleResult = BattleResult_cgd.Draw

    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        ' check for unresolved
        If OKToClose() Then
            Me.Hide()
        End If

    End Sub

    Private Function OKToClose() As Boolean
        Dim battKey As String
        Dim batt As Battle
        Dim bUnresolved As Boolean = False

        For Each battKey In ptrGameData.Battles_g.HT.Keys
            batt = ptrGameData.Battles_g.Item(battKey)
            If batt.BattleResult = BattleResult_cgd.NotFinished Then
                bUnresolved = True
                Exit For
            End If
        Next

        If bUnresolved Then
            If MessageBox.Show("There are battles listed that have not been completed, they will be marked as a DRAW if you continue. Do you wish to continue.", "Default To Draw?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
                ' set all battles that are unfinished to DRAW.
                For Each battKey In ptrGameData.Battles_g.HT.Keys
                    batt = ptrGameData.Battles_g.Item(battKey)
                    If batt.BattleResult = BattleResult_cgd.NotFinished Then
                        batt.BattleResult = BattleResult_cgd.Draw
                    End If
                Next
                Return True
            Else
                Return False
            End If
        Else
            ' no unresolved battles
            Return True
        End If

    End Function

End Class

Public Class BattleTreeNode
    Inherits TreeNode

    Private m_BattleId As Integer

    Public Sub New(ByVal Text As String)
        MyBase.Text = Text
    End Sub

    Public Property BattleID() As Integer
        Get
            Return m_BattleId
        End Get
        Set(ByVal Value As Integer)
            m_BattleId = Value
        End Set
    End Property

End Class